package com.server.adapter;

import java.util.Calendar;
import java.util.Vector;

import com.data.Interview;
import com.data.User;

public abstract class AbstractJobmineAdapter {
	/*
	 * Creates job mine adapter
	 */
	public static AbstractJobmineAdapter createAdapter() {
		return new SeleniumAdapter(); 
	}

	/*
	 * retrieves interview data
	 * 
	 * @param id User id
	 * @param pwd User password
	 */
	public Vector<Interview> getInterview(String id, String pwd) {
		Vector<Interview> interviewList = new Vector<Interview>();

		/*
		 * get html src from jobmine interview page
		 */
		String htmlSrc = getHTML(id, pwd);

		/*
		 * call htmlParser to parse the html source retrieved from jobmine
		 */
		HTMLParser htmlParser = new HTMLParser(htmlSrc);
		String[] interviewData = htmlParser.getInterviews();

		/*
		 * if HTMLParser error(wrong userId, Pwd combination), return null
		 */
		if (interviewData == null) {
			return null;
		} else if (interviewData.equals("")) { 
			/*
			 * if there is no interview, return an empty interview list
			 */
			return interviewList;
		}
		
		/*
		 *  build interview data objects
		 */
		Interview[] interviews = new Interview[interviewData.length];

		for (int i = 0; i < interviewData.length; i++) {
			String[] currentInterview = interviewData[i].split("\\,");

			/*
			 * assign values to interview info
			 */
			User currentUser = new User(id, pwd);
			interviews[i] = new Interview(currentUser, currentInterview[1], currentInterview[4]);
			interviews[i].setEmployerName(currentInterview[2]);
			interviews[i].setInterviewer(currentInterview[10]);
			interviews[i].setJobTitle(currentInterview[3]);
			Calendar currentDate = Calendar.getInstance();
			interviews[i].setLastUpdateTime(currentDate.getTime());
			interviews[i].setLength(Integer.parseInt(currentInterview[8]));
			interviews[i].setLocation(currentInterview[9]);
			interviews[i].setStatus(currentInterview[11]);
			interviews[i].setType(currentInterview[5]);

			interviewList.add(interviews[i]);
		}

		return interviewList;
	}

	/*
	 * get HTML src from the interview page on Jobmine
	 *
	 * @param id User id
	 * @param pwd User password
	 */
	public abstract String getHTML(String id, String pwd);
}